<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索添加 -->
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getFactoryList">
            <el-button slot="append" icon="el-icon-search" @click="getFactoryList"></el-button>
          </el-input>
        </el-col>

      </el-row>
      <!-- 用户列表 -->
      <el-table :data="factoryList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="工厂名" prop="fa_name"></el-table-column>
        <el-table-column label="工厂介绍" prop="fa_desc"></el-table-column>
        <el-table-column label="负责人" prop="fa_spon"></el-table-column>
        <el-table-column label="联系电话" prop="fa_conn"></el-table-column>
        <el-table-column label="状态" prop="fa_state"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-close"  @click="shutFactory(scope.row.fa_id)" circle></el-button>
            <el-button type="success" icon="el-icon-check" @click="startFactory(scope.row.fa_id)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 请求数据
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5
      },
      factoryList: [],// 用户列表

      total: 0, // 最大数据记录
      addDialogVisible: false ,// 对话框显示
    };
  },
  created() {
    this.getFactoryList();
  },
  methods: {
    async getFactoryList() {
      // 调用post请求
      const { data: res } = await this.$http.get("factory/factoryList", {
        params: this.queryInfo
      });
      for(var i in res.data){
        if(res.data[i].fa_state === 0){
          res.data[i].fa_state = '已关闭';
        }else if(res.data[i].fa_state === 1){
          res.data[i].fa_state = '已开启';
        }
      }
       // 将返回数据赋值
      this.factoryList = res.data;

      this.total = res.numbers; // 总个数
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getFactoryList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getFactoryList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.addFormRef.resetFields();// 重置表单项
    },
    //修改工厂状态
    async shutFactory(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将关闭工厂, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消修改");
      }
      const {data:res} = await this.$http.get("factory/shutFactory?id="+id);
      if (res !== "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getFactoryList();
    },
    //开启工厂状态
    async startFactory(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将打开工厂, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消修改");
      }
      const {data:res} = await this.$http.post("factory/startFactory?id="+id);
      if (res !== "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getFactoryList();
    },
  }
};
</script>
<style lang="less" scoped>
</style>